import request from '../../utils/request';
import API from '../../config/API';

let startY = 0;         // 手指起始的纵坐标
let moveY = 0;          // 手指移动的纵坐标
let moveDistance = 0;   // 手指移动的距离

Page({
    data: {
        moveDistance: 0,    // 位移距离
        transition: '',     // 动画 transition
        loginUser: null,    // 用户数据
        recordList: null,   // 用户播放记录
    },
    handleTouchStart(e) {
        this.setData({ transition: '' });
        startY = e.touches[0].clientY;
    },
    handleTouchMove(e) {
        moveY = e.touches[0].clientY;
        moveDistance = moveY - startY;
        if (moveDistance < 0) {
            return;
        } else if (moveDistance > 80) {
            moveDistance = 80;
        }
        this.setData({ moveDistance  });
    },
    handleTouchEnd() {
        this.setData({ moveDistance: 0, transition: 'transform 0.5s linear' });
    },
    // 前端登录页面
    toLogin() {
        wx.navigateTo({ url: '/pages/login/login' });
    },
    // 获取用户最近的播放记录
    async getUserRecentPlayList(uid, type = 1) {
        let resp = await request(API.recordList, { uid, type });
        if (resp.code === 200) {
            let index = 0;
            let recordList = resp.weekData.splice(0, 10);
            recordList.forEach(item => item.id = index++);
            this.setData({ recordList });
        } else {
            wx.showToast({ title: resp.message, icon: 'none' });
        }
    },
    /** 13989397447
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 获取用户登录数据
        let loginUser = wx.getStorageSync('loginUser');
        if (loginUser) {
            this.setData({ loginUser: JSON.parse(loginUser) });
            // 获取用户播放记录
            this.getUserRecentPlayList(this.data.loginUser.userId);
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})